package android.ijiajiao.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.Part;

import android.ijiajiao.dao.VideoDao;
import android.ijiajiao.util.GetDateUtil;
import android.ijiajiao.util.GetIpUtil;
import android.ijiajiao.util.JDBCConnectUtil;
import android.ijiajiao.util.SaveVideoUtil;

public class VideoDaoImpl implements VideoDao {

	/**
	 * 添加精彩视频
	 */
	@Override
	public boolean AddVideo(String title, String autor, String video_summary, Part part, String path) {
		// TODO Auto-generated method stub
		/**
		 * video_autor varchar(20)not null, video_title varchar(100) not null,
		 * video_summary varchar(1000) not null, video_time datetime not null,
		 * video_adr varchar(200) not null
		 */
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "insert into video(video_title,video_autor,video_summary,video_time,video_adr) values (?,?,?,?,?)";
		boolean falg = false;
		try {

			String savePath = GetIpUtil.getip() + ":8080/AndroidiJiaJiao/data/video/";
			String video_adr = new SaveVideoUtil().saveVideo(path, part, savePath);
			ps = conn.prepareStatement(sql);
			ps.setString(1, title);
			ps.setString(2, autor);
			ps.setString(3, video_summary);
			ps.setString(4, GetDateUtil.getdate());
			ps.setString(5, video_adr);
			int row = ps.executeUpdate();
			if (row > 0) {
				falg = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDBCConnectUtil.close(null, null, ps, conn);
		}
		return falg;
	}

	/**
	 * 视频列表
	 */
	@Override
	public ArrayList<Object> VideoList() {
		// TODO Auto-generated method stub
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select video.video_id,video.video_title,video.video_autor,video.video_time,user.img,collect_num from video inner  join  user  on video.video_autor=user.name   order by video.video_time desc";

		ArrayList<Object> data = new ArrayList<>();
		ArrayList<String> list = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				list = new ArrayList<>();
				list.add(rs.getString(1));
				list.add(rs.getString(2));
				list.add(rs.getString(3));
				list.add(rs.getString(4));
				list.add(rs.getString(5));
				list.add(rs.getString(6));
				data.add(list);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);

		}

		return data;

	}

	/**
	 * 精彩视频详细信息
	 */
	@Override
	public ArrayList<Object> VideoDetail(String video_id) {
		// TODO Auto-generated method stub/**
		/**
		 * video_id int not null auto_increment unique primary key, video_autor
		 * varchar(20)not null, video_title varchar(100) not null, video_summary
		 * varchar(1000) not null, video_time datetime not null, video_adr
		 * varchar(200) not null
		 */
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select video.video_adr,video.video_title,video.video_autor,video.video_time,video.video_summary,user.img,collect_num from video inner  join  user  on video.video_autor=user.name  where video.video_id=?";
		ArrayList<Object> listdata = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, video_id);
			rs = ps.executeQuery();
			while (rs.next()) {
				listdata.add(video_id);
				listdata.add(rs.getString(1));
				listdata.add(rs.getString(2));
				listdata.add(rs.getString(3));
				listdata.add(rs.getString(4));
				listdata.add(rs.getString(5));
				listdata.add(rs.getString(6));
				listdata.add(rs.getString(7));
			}

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);

		}
		return listdata;
	}

	@Override
	public ArrayList<String> collectvideo(String name, String video_id) {
		ArrayList<String> data = new ArrayList<>();
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "insert into collect_video(name,video_id) values (?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, video_id);
			int row = ps.executeUpdate();
			if (row > 0) {
				String sql1 = "update video set collect_num=collect_num+1 where video_id=?";
				if (ps != null) {
					ps.close();
				}
				ps = conn.prepareStatement(sql1);
				ps.setString(1, video_id);
				int row1 = ps.executeUpdate();
				if (row1 > 0) {
					String sql2 = "select collect_num from video where video_id=?";
					if (ps != null) {
						ps.close();
					}
					ps = conn.prepareStatement(sql2);
					ps.setString(1, video_id);
					rs = ps.executeQuery();
					if (rs.next()) {
						data.add(rs.getString(1));
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);
		}
		return data;
	}

	@Override
	public ArrayList<Object> collected(String name) {
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select video.video_id,video.video_title,video.video_autor,video.video_time,user.img,collect_num from video inner  join  user  on video.video_autor=user.name and video_id in (select video_id from collect_video where name=?)  order by video.video_time desc";

		ArrayList<Object> data = new ArrayList<>();
		ArrayList<String> list = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			while (rs.next()) {
				list = new ArrayList<>();
				list.add(rs.getString(1));
				list.add(rs.getString(2));
				list.add(rs.getString(3));
				list.add(rs.getString(4));
				list.add(rs.getString(5));
				list.add(rs.getString(6));
				data.add(list);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);

		}

		return data;

	}
}
